/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	protocol_stack.c
**  Description:	
**  Author:        	 
**  Version				v0.1
**  Date:           	2019-06-29
**	Function List： 
**  History:         
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/
#include "tcp_transfer.h"
#include "system_cfg.h"



/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                              	结构体定义
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	函数原型声明
*********************************************************************************************************
*/

static uint8_t get_frame_tail(void* p_pack);
/*
*********************************************************************************************************
*                                              	全局变量定义
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                              	模块静态变量定义
*********************************************************************************************************
*/
static protocol_handler_node_t * p_handler_head=NULL;


/*
*********************************************************************************************************
*                                              	函数定义
*********************************************************************************************************
*/

void protocol_init(void)
{
	return;
}
void protocol_input(protocol_cmd_t cmd,uint8_t* buff,uint16_t size)
{
	/* 协议处理 */
	protocol_handler_node_t* p_node = p_handler_head;
	
	while(p_node != NULL)
	{
		if(p_node->cmd == cmd && p_node->pfn_handler != NULL)
		{
			p_node->pfn_handler(cmd,buff,size);
			return;
		}
		p_node = p_node->p_next;
	}
	ErrorLog("Receive undefined cmd 0x%02x!\r\n",cmd);
	return;
}

//void upld_supply_consume(uint16_t supply_type)
//{
//	tcp_transfer_push(UPLD_SUPPLY_CONSUME,(uint8_t*)&supply_type,sizeof(uint16_t));
//}

protocol_handler_node_t* protocol_handler_register(protocol_cmd_t cmd,pfn_handler_callback pfn_handler)
{
	if(pfn_handler == NULL)
		return NULL;
	
	rt_base_t it = rt_hw_interrupt_disable();
	protocol_handler_node_t* p_node = NULL;
	p_node = malloc(sizeof(protocol_handler_node_t));
	if(p_node == NULL)
	{
		Trace_Log("Protocol handler memalloc failed!\r\n");
	}
	else{
		memset(p_node,0,sizeof(protocol_handler_node_t));
		p_node->cmd = cmd;
		p_node->pfn_handler = pfn_handler;
		
		/*反正顺序无意义 直接在p_handler_head头添加吧*/
		if(p_handler_head == NULL)
		{
			p_handler_head = p_node;
		}
		else
		{
			p_node->p_next = p_handler_head;
			p_handler_head = p_node;
		}
	}
	rt_hw_interrupt_enable(it);
	return p_node;
}
uint16_t endian_short(uint16_t arg){
	return	arg;
	uint16_t rlt=(arg<<8) + (arg>>8);
	return	rlt;
}
uint32_t endian_int(uint32_t	arg){
	return	arg;
	uint32_t rlt=(arg<<24) + (arg<<8)&0x00FF0000 + (arg>>24) + (arg>>8)&0x0000FF00;
	return	rlt;
}